var express = require('express');
var router = express.Router();
const seatsServices = require("../src/services/seatsServices");

router.get('/allseats', async function (req, resp) {
  let result=await seatsServices.Allseatss()
  resp.json({
    "status": 0,
    "message": "",
    "total": 180,
    "datas":result
  })
}
);

//查找详细信息
router.get('/seatsDatail/:id', async function (req, resp) {
  let {id}=req.params;
  let result=await seatsServices.completeInformation(id)
  if (result != null) {
    resp.json({
      'code': 1,
      'msg': '查询成功',
      'data': result
    })
  } else {
    resp.json({
      'code': 0,
      'msg': '查询失败'
    })
  }
}
);
router.post('/selectSeat', async function (req, resp) {
  let {row,col,displayName,theaterId} = req.body;
  let res = await seatsServices.checkIn({row,col,displayName,theaterId});
  console.log(res);
  if (res.code==1) {
    resp.json({
      "code": 1,
      "msg": "选座成功",
      'data':res
    })
  } else {
    resp.json({
      "code": 0,
      "msg": "选座失败",
      'data':res
    })
  }
});
//修改
router.put('/modifyPosition', async function (req, resp) {
  let {id,row,col,displayName,theaterId} = req.body;
  let res = await seatsServices.modifyOption({'_id':id},{row,col,displayName,theaterId});
  console.log(res);
  if (res.modifiedCount==1) {
    resp.json({
      "code": 1,
      "msg": "修改成功",
      'data':res
    })
  } else {
    resp.json({
      "code": 0,
      "msg": "修改失败",
      'data':res
    })
  }
});

//删除位置
router.delete('/deleteLocation/:id', async function (req, resp) {
  let {id} = req.params;
  let res = await seatsServices.deleteSelectedSeat(id);
  
  
  if (res.deletedCount==1) {
    resp.json({
      "code": 1,
      "msg": "修改成功"
      
    })
  } else {
    resp.json({
      "code": 0,
      "msg": "修改失败"
      
    })
  }
});

//分页
router.get('/page', async function (req, resp) {
  let {
    currentpage,
    pagesize
  } = req.query;

  console.log(currentpage, pagesize);

  let pageBean = await seatsServices.getAccessToPag({
    "currentpage": parseInt(currentpage),
    "pagesize": parseInt(pagesize)
  })

  resp.json({
    "status": 0,
    "message": "success",
    ...pageBean,
  })
});

module.exports = router